package com.ronri.ams;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.ronri.common.UrlEncode;
import com.ronri.db.RemoteDataService;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/5/30.
 */
public class activity_opinion extends AutoLayoutActivity {

    @BindView(R.id.title_text_tv)
    TextView titleTextTv;
    @BindView(R.id.title_text_left)
    TextView titleTextLeft;
    @BindView(R.id.btnSure)
    RelativeLayout btnSure;
    @BindView(R.id.btnBack)
    RelativeLayout btnBack;
    @BindView(R.id.editText)
    EditText edContent;
    @BindView(R.id.button2)
    Button btOK;
    private Unbinder mUnbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opinion);
        mUnbinder = ButterKnife.bind(this);

        titleTextTv.setText("意见反馈");

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //意见反馈
        btOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RemoteDataService remoteDataService = new RemoteDataService();

                // 获取远程地址
                String szRemoteAddress = "http://120.55.93.238:8080/ams/api/feedback.json";
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("user_id", InitApplication.LoginInfo.getData().getUser_id());
                jsonObject.put("feedback", edContent.getText().toString());

                Map<String, Object> mParam = new HashMap<String, Object>();

                try {
                    mParam.put("addFrom", 1);
                    mParam.put("addMark", "android");
                    mParam.put("json", jsonObject.toString());
                } catch (Exception ex) {

                }

                Log.i("test", "mParam-->" + mParam.toString());
                // 获取远程数据
                remoteDataService.getRemoteData(szRemoteAddress,
                        UrlEncode.getUrlParamsByMap(mParam, false),
                        InitApplication.SystemConfig.getModule().getLogin().getMethod(),
                        new RemoteDataService.OnRemoteDataEvent() {

                            @Override
                            public void OnResultEvent(String szString) {
                                Log.i("test", "szString-->" + szString);
                                if (szString.contains("网络连接失败")) {
                                    Toast.makeText(activity_opinion.this, "网络链接失败", Toast.LENGTH_SHORT).show();
                                    return;
                                }

                                JSONObject jsonObject = JSON.parseObject(szString);

                                if (jsonObject.getBoolean("success")) {
                                    Toast.makeText(activity_opinion.this, "感谢您的反馈", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(activity_opinion.this, "反馈失败", Toast.LENGTH_SHORT).show();
                                }
                                finish();
                            }

                            @Override
                            public void OnRuningState(int nStatus) {

                            }

                            @Override
                            public String OnGetCustomParam() {
                                return null;
                            }
                        });
            }
        });
    }


    public void getRemoteData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
}
